/**
 * @fileoverview 为Count服务的reducer
 */

// 初始化state
const initState = 0

export const types = {
  INCREMENT: 'increment',
  DECREMENT: 'decrement'
}

const reducer = (preState = initState, action) => {
  const { data, type } = action
  switch (type) {
    case types.INCREMENT:
      return preState + data
    case types.DECREMENT:
      return preState - data

    default:
      return preState
  }
}

export default reducer

export const actions = {
  increment: data => ({ type: types.INCREMENT, data }),
  decrement: data => ({ type: types.DECREMENT, data }),
  incrementAsync: (data, time) => {
    return dispatch => {
      setTimeout(() => {
        dispatch(actions.increment(data))
      }, time)
    }
  }
}
